<?php
ini_set("display_errors", 0);

error_reporting(E_ALL ^ E_NOTICE);

error_reporting(E_ALL ^ E_WARNING);

header("Content-type: text/html; charset=utf-8");

//链接数据库
/*$servername = 'localhost';
$username = 'root';  //用户名
$password = 'root';  //密码
$dbname = "ejfx";   //数据库
$conn=mysql_connect($servername, $username, $password) or die('连接失败');
mysql_select_db($dbname);
mysql_query('set names utf8');*/

$servername = 'localhost';
$username = 'sql_09xingfuba2_';  //用户名
$password = 'M2MdJCrJyzC5fJ2W';  //密码
$dbname = "sql_09xingfuba2_";   //数据库
$conn=mysql_connect($servername, $username, $password) or die('连接失败');
mysql_select_db($dbname);
mysql_query('set names utf8');

//每日收益
$time = time();
//类型一
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;


$res = mysql_query("SELECT * FROM erj_userkj WHERE  type = 1 AND jstime < $beginToday AND status = 0 AND addtime < $beginToday");
while($user = mysql_fetch_array($res)){
    $user_kj[] = $user;
}


$jstime = mysql_fetch_array(mysql_query("SELECT MAX(jstime) FROM erj_userkj "));
$jstime = $jstime[0];


if(!$user_kj){
    die("无可执行记录！");
}

//类型一
if($user_kj){
    foreach($user_kj as $k=>$v){
        $user_id = $v["user_id"];
        $kj_id = $v["kj_id"];
        $licai_id = $v["id"];
        $user = mysql_fetch_array(mysql_query("SELECT * FROM erj_user WHERE  id =  ".$user_id));
        $kuangji = mysql_fetch_array(mysql_query("SELECT * FROM erj_kuangji WHERE  id =  ".$v['kj_id']));
        if($v['endtime'] > $time){  //矿机未到期
            //收益
            $shouyi = $kuangji['day_num'];

            //进入余额
            $usermoney = $user['usermoney']+$shouyi;

            //更新用户余额
            mysql_query("UPDATE erj_user SET usermoney = $usermoney WHERE id = ".$v['user_id']);
            mysql_query("UPDATE erj_userkj SET jstime = $time WHERE id = ".$v['id']);

            //插入记录
            $msg =  '矿机收益，矿机编号-KJ'.$v['addtime'];
            $money = $shouyi;
            mysql_query("INSERT INTO erj_log (user_id,money,amount,type , msg,licai_id, date) VALUES ($user_id,$money,$shouyi,22,'$msg',$licai_id,$time)");

        }else{ //矿机到期，返还本金
            if($kuangji['shifang'] == 1){
                $usermoney = $user['usermoney'] + $v['price'];
                mysql_query("UPDATE erj_user SET usermoney = $usermoney WHERE id = ".$user_id);
                mysql_query("UPDATE erj_userkj SET status = 1 ,jstime = $time WHERE id = ".$v['id']);
                //插入记录
                $msg =  '矿机到期，返还本金';
                $money = $v['price'];
                $suc = mysql_query("INSERT INTO erj_log (user_id,money,amount,type ,  msg, date) VALUES ($user_id,$money,0,24,'$msg',$time)");
            }else{
                $suc = mysql_query("UPDATE erj_userkj SET status = 1  ,jstime = $time WHERE id = ".$v['id']);
            }
        }

    }
}

//恢复矿机每日限购数
//mysql_query("UPDATE erj_kuangji SET now_num = 0 ,now_money = 0 ");
die("理财收益已结算！");



?>